﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "GZoomFreeTransformationComponent.h"
#include "IElement.h"
#include "DbObjectUtils.h"
#include "Vector3dUtils.h"
#include "IElementStatus.h"
#include "IGraphicsZoomFree.h"
#include "IElementModelShape.h"
#include "IPureGraphicsElement.h"
#include "IGraphicsElementShape.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace Sample;

DBOBJECT_DATA_DEFINE(GZoomFreeTransformationComponent)
{
    SetOwnerElement(nullptr);
}

bool GZoomFreeTransformationComponent::Transform(const Matrix4d& matrix)
{
    IPureGraphicsElement* pPGE = quick_cast<IPureGraphicsElement>(GetOwnerElement());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pPGE, L"所属Element不能为空",L"GDMP",L"2024-03-30");

    IElementModelShape* pElementModelShape = pPGE->GetElementModelShape();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pElementModelShape, L"pElementModelShape不能为空",L"GDMP",L"2024-03-30");
    const IGraphicsElementShape* pGRep = pElementModelShape->GetGraphicsElementShape();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pGRep, L"pGRep不能为空",L"GDMP",L"2024-03-30");

    // 复制一份GRep做修改，然后设置回去
    OwnerPtr<IGraphicsElementShape> opNewGRep = TransferOwnershipQuickCast<IGraphicsElementShape>(pGRep->Clone());

    for (auto& itor : opNewGRep->GetChildrenFw())
    {
        IGraphicsZoomFree* pGZoomFree = quick_cast<IGraphicsZoomFree>(itor.get());
        if(!pGZoomFree)continue;

        // 用ZoomFree原来的Coordinate乘以matrix
        Coordinate3d coord = pGZoomFree->GetCoordinate();
        coord.Transform(matrix);
        pGZoomFree->SetCoordinate(coord);
    }

    pElementModelShape->SetGraphicsElementShape(TransferOwnership(opNewGRep));
    return true;
}

bool GZoomFreeTransformationComponent::Translate(const gcmp::IElementMoveContext& moveContext)
{
    Vector3d move = moveContext.GetMoveVector();
    Matrix4d moveMatrix;
    moveMatrix.MakeTranslate(move);
    return Transform(moveMatrix);
}

bool GZoomFreeTransformationComponent::Rotate(const gcmp::Vector3d& origin, const gcmp::Vector3d& axis, double angle)
{
    Matrix4d rotMat;
    rotMat.MakeRotate(origin, axis, angle);
    return Transform(rotMat);
}

bool GZoomFreeTransformationComponent::CanBeMoved(std::wstring* explanation /*= nullptr*/) const
{
    const IElement* pElement = GetOwnerElement();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pElement, L"所属Element不能为空",L"GDMP",L"2024-03-30");
    return pElement->GetStatus()->CanBeMoved();
}

bool GZoomFreeTransformationComponent::CanBeRotated(std::wstring* explanation /*= nullptr*/) const
{
    const IElement* pElement = GetOwnerElement();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pElement, L"所属Element不能为空",L"GDMP",L"2024-03-30");
    return pElement->GetStatus()->CanBeRotated();
}

bool GZoomFreeTransformationComponent::Scale(const gcmp::Vector3d& basePt, float scaleFactor)
{
    return false;
}

gcmp::NdbObject* GZoomFreeTransformationComponent::GetTopOwnerObject() const
{
    return quick_cast<gcmp::NdbObject>(m_pOwnerElement);
}

